package com.ibm.framework.exception.handler.monitor;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.ibm.framework.exception.handler.ExceptionInfoFormater;
import com.ibm.framework.exception.model.ExceptionInfo;

/**
 * 
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉 异常监控信息的格式化工具
 * 
 * @author zhaopin
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class MonitorMessageFormater implements ExceptionInfoFormater {
    /**
     * 返回格式化后的字符串
     * 
     * @param expInfo 异常信息
     * @return 格式化后的字符串
     */
    public String getFormatString(ExceptionInfo expInfo) {

        Map<String, String> msgMap = new HashMap<String, String>();
        if (expInfo instanceof MonitorExceptionInfo) {
            MonitorExceptionInfo temp = (MonitorExceptionInfo) expInfo;
            msgMap.put("systemId", temp.getSystemId());
            msgMap.put("appId", temp.getAppId());
            msgMap.put("serverIp", temp.getServerIp());
        }

        String errorMsg = (null == expInfo.getExceptionMessage()) ? "" : expInfo.getExceptionMessage();
        String errorCode = (null == expInfo.getCode()) ? "" : expInfo.getCode();
        msgMap.put("errorMsg", errorMsg);
        msgMap.put("errorCode", errorCode);
        msgMap.put("errorTime", expInfo.getInterceptTime());

        // use Gson to transform ExceptionInfo to JSON String
        Gson gson = new Gson();
        return gson.toJson(msgMap);
    }
}
